package com.jr.service.impl;

import com.jr.entry.BlogText;
import com.jr.entry.User;
import com.jr.mapper.BlogTextMapper;
import com.jr.mapper.UserMapper;
import com.jr.pojo.ShowBlog;
import com.jr.service.BlogTextService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class BlogTextServiceImpl implements BlogTextService {
    @Autowired
    private BlogTextMapper blogTextMapper;

    @Autowired
    private User user;
    @Autowired
    private UserMapper userMapper;

    @Override
    public ArrayList<ShowBlog> showBlogTextBy(BlogText blogText, RowBounds rowBounds) {
        return blogTextMapper.selectBy(blogText, rowBounds);
    }

    @Override
    public ShowBlog showTextByBlogId(Integer blogId) {
        RowBounds rb = new RowBounds(0, 1);
        BlogText blogText = new BlogText();
        blogText.setBlogid(blogId);
        ArrayList<ShowBlog> blogTexts = blogTextMapper.selectBy(blogText, rb);
        Integer clicknumber = blogTexts.get(0).getClicknumber();
        blogText.setClicknumber(clicknumber + 1);
        blogTextMapper.updateBlogTextByBlogId(blogText);
        return blogTexts.get(0);
    }

    @Override
    public ArrayList<BlogText> showByStatus(Integer status) {
        return blogTextMapper.selectByStatus(status);
    }

    @Override
    public ArrayList<ShowBlog> showBlogTextByClickNumber() {
        return blogTextMapper.selectByClickNumber();
    }

    @Override
    public Integer showBlogTextSize(BlogText blogText) {
        return blogTextMapper.selectBlogSize(blogText);
    }

    @Override
    public BlogText showBlogTextDetail(BlogText blogText) {
        return null;
    }

    @Override
    public ArrayList<BlogText> showSuggest(BlogText blogText) {
        return null;
    }

    @Override
    public ArrayList<ShowBlog> showBackground(BlogText blogText,RowBounds rowBounds) {
        return blogTextMapper.selectByUid(blogText,rowBounds);
    }

    @Override
    public ArrayList<ShowBlog> showBackground1(BlogText blogText) {
        return blogTextMapper.selectByblogid(blogText);
    }
    @Override
    public boolean editBlogText(BlogText blogText) {
        return blogTextMapper.updateBlogTextByBlogId(blogText)==1?true:false;
    }

    @Override
    public boolean removeBlogText(BlogText blogText) {
        return blogTextMapper.deleteBlogTextByBlogId(blogText)==1?true:false;
    }

    @Override
    public boolean addBlogText(BlogText blogText) {

        user.setUid(blogText.getUid());
        blogText.setAuthor( userMapper.selectByuid(user).getUsername());
        return blogTextMapper.insertBlogText(blogText)==1?true:false;
    }
}
